import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'use-http';
import { ToastContainer, toast } from 'react-toastify';
import Application from './App';
import * as serviceWorker from './serviceWorker';
import 'bootstrap/dist/css/bootstrap.min.css';
import 'react-toastify/dist/ReactToastify.css';

const ApplicationContainer = ({ children }) => {
  const options = {
    interceptors: {
      response: async ({ response }) => {
        if (!response.ok) {
          toast.error(response.statusText);
        }
        return response;
      }
    }
  };

  return <Provider options={options}>{children}</Provider>;
};

ReactDOM.render(
  <React.StrictMode>
    <ToastContainer autoClose={2000} hideProgressBar />
    <ApplicationContainer>
      <Application />
    </ApplicationContainer>
  </React.StrictMode>,
  document.getElementById('root')
);

// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
// Learn more about service workers: https://bit.ly/CRA-PWA
serviceWorker.unregister();
